//
//  LLRefresh.swift
//  下拉刷新
//
//  Created by 任玉林 on 16/7/1.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

enum refreshType: Int{
    //正常
    case Namole = 0
    //下拉中
    case Pulling = 1
    //刷新中
    case refreshing = 2
    
}

let refreshH: CGFloat = 50

let refreshW:CGFloat = UIScreen.mainScreen().bounds.width

class LLRefresh: UIControl {
    
    //定义被观察者
    var scrollView: UIScrollView?
    
    var refreshState: refreshType = .Namole {
        
        didSet{
            
            switch refreshState {
                
            case .Namole:
                messageLable.text = "正常"
                
                if oldValue == .refreshing{
                    
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        
                        self.scrollView?.contentInset.top -= refreshH
                        
                    })
                }
                
            case .Pulling:
                messageLable.text = "下拉中"
                
            case.refreshing:
                messageLable.text = "刷新中"
                
               UIView.animateWithDuration(0.25, animations: { () -> Void in
                
                self.scrollView?.contentInset.top += refreshH
                
                
                }, completion:{ (_) -> Void in
                
                    self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
                
                })
                
                
            }
            
            
        }
        
        
    }
    
    // MARK: - 供外界调用方法
    func endRefreshing(){
        
        refreshState = .Namole
    }
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: -refreshH, width: refreshW, height: refreshH))
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        backgroundColor  = UIColor.orangeColor()
        addSubview(messageLable)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        messageLable.frame = self.bounds
        
    }
    
    private lazy var messageLable: UILabel = {
        
        let lab = UILabel()
        
        lab.textAlignment = .Center
        
        lab.textColor = UIColor.whiteColor()
        
        lab.font = UIFont.systemFontOfSize(15)
        
        lab.text = "正常"
        
        return lab
    }()
    
    //检测本类将要添加到那个view上
    override func willMoveToSuperview(newSuperview: UIView?) {
        
        guard let scrollView = newSuperview as? UIScrollView else{
            
            return
        }
        
        self.scrollView = scrollView
        
        //注册观察者 KVO
        scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
        
    }
    
    //观察者实现
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        
        let contenOffSetY = self.scrollView!.contentOffset.y
        
        let contenOffSetTop = self.scrollView!.contentInset.top
        
        if scrollView!.dragging{
        
        if contenOffSetY > -contenOffSetTop - refreshH && refreshState == .Pulling {
            
            refreshState = .Namole
            
            
        }else if contenOffSetY <= -contenOffSetTop - refreshH && refreshState == .Namole  {
            
            refreshState = .Pulling
            
            }
        }else{
            
            if refreshState == .Pulling{
                
                refreshState = .refreshing
            }
        }
        
    }
    
    //移除观察者
    deinit{
        self.scrollView?.removeObserver(self, forKeyPath: "contentOffset")
        
    }
    
}
